package Web.Servlet;

import Domain.*;
import Service.CartService;
import Service.Impl.CartServiceImpl;
import Service.Impl.OrderServiceImpl;
import Service.Impl.ProductServiceImpl;
import Service.Impl.UserServiceImpl;
import Service.OrderService;
import Service.ProductService;
import Service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "OrdersServlet", value = "/OrdersServlet")
public class OrdersServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        List<Product>product=new ArrayList<>();
        List<Cart> carts=new ArrayList<>();
        User user=new User();
        String action = request.getParameter("action");

        if(action.equals("buyorders")){
            CartService cartService=new CartServiceImpl();
            String[] cids = request.getParameterValues("cid");
           carts=cartService.getCarts(cids);
           List<String> ids=new ArrayList<String>();

            for (Cart c:carts) {
               ids.add(String.valueOf(c.getProductid()));
            }
            ProductService service=new ProductServiceImpl();
            product=service.getProducts(ids);
            String uid=request.getParameter("uid");
            UserService service2 = new UserServiceImpl();
            List<User> list1 = service2.findUserById(Integer.parseInt(uid));
            user = list1.get(0);

            carts=cartService.getCarts(cids);
            request.setAttribute("products",product);
            request.setAttribute("user",user);
            request.setAttribute("carts",carts);
            request.getRequestDispatcher("Orders/settlement.jsp").forward(request,response);
        }

        if(action.equals("newOrder")){

            int payid=Integer.parseInt(request.getParameter("pay"));
            String[] ids = request.getParameterValues("ppid");
            List<String> p=new ArrayList<>();
            for (String id : ids) {
                p.add(id);
            }
            ProductService service=new ProductServiceImpl();
            product=service.getProducts(p);
            OrderService orderService=new OrderServiceImpl();
            String uid=request.getParameter("uid");
            String[] cids = request.getParameterValues("ccid");
            UserService service2 = new UserServiceImpl();
            List<User> list1 = service2.findUserById(Integer.parseInt(uid));
            user = list1.get(0);
            CartService cartService=new CartServiceImpl();
            carts=cartService.getCarts(cids);
            orderService.insertorders(product,user,carts,payid);
            List<Cart> cc=cartService.FindCartByUserid(user.getId());
            request.setAttribute("product",cc);
            request.setAttribute("user",user);
            request.getRequestDispatcher("Cart/CartPage.jsp").forward(request,response);
        }

        if(action.equals("buynow")){
            String productid=request.getParameter("pid");
            String userid=request.getParameter("uid");
            String buynums=request.getParameter("buynum");
            UserService service2 = new UserServiceImpl();
            List<User> list1 = service2.findUserById(Integer.parseInt(userid));
            user = list1.get(0);
            ProductService productService=new ProductServiceImpl();
            List<Product> list2=productService.FindProductById(Integer.parseInt(productid));
            Product products=list2.get(0);
            request.setAttribute("product",products);
            request.setAttribute("user",user);
            request.setAttribute("buynum",buynums);
            request.getRequestDispatcher("Orders/settlementBuyone.jsp").forward(request,response);
        }

        if(action.equals("newoneOrder")){
            String productid=request.getParameter("pid");
            String userid=request.getParameter("uid");
            int buynums=Integer.parseInt(request.getParameter("buynum"));
            int payid=Integer.parseInt(request.getParameter("pay"));
            UserService service2 = new UserServiceImpl();
            List<User> list1 = service2.findUserById(Integer.parseInt(userid));
            user = list1.get(0);
            ProductService productService=new ProductServiceImpl();
            List<Product> list2=productService.FindProductById(Integer.parseInt(productid));
            Product products=list2.get(0);
            OrderService orderService=new OrderServiceImpl();
            orderService.buyone(products,user,buynums,payid);
        }

        if(action.equals("Orderselect")){

            int uid=Integer.parseInt(request.getParameter("uid"));

            OrderService orderService=new OrderServiceImpl();
            List<Orders> orders=orderService.selectorderbyuserid(uid);
            UserService service2 = new UserServiceImpl();
            List<User> list1 = service2.findUserById(uid);
            user = list1.get(0);
            request.setAttribute("pb",orders);
            request.setAttribute("user",user);
            request.getRequestDispatcher("Orders/Orderselect.jsp").forward(request,response);
        }

        if(action.equals("new")){
            OrderService service=new OrderServiceImpl();
            String ooid=request.getParameter("ooid");
            String uid = request.getParameter("uid");

            UserService service1 = new UserServiceImpl();
            List<User> list = service1.findUserById(Integer.parseInt(uid));
            User user1 = list.get(0);
            List<topr2> tp=service.getoandpselect();
            List<Orderselect> os=service.getorderselect(Integer.parseInt(ooid));

            request.setAttribute("os",os);
            request.setAttribute("ooid",ooid);
            request.setAttribute("tp",tp);
            request.setAttribute("userid",uid);

            request.getRequestDispatcher("Orders/Popup.jsp").forward(request,response);
        }

    }
}
